package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseMVPActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.persenter.ChangerPasswordPersenter;
import com.shanling.shanlingcontroller.persenter.contract.ChangePasswordContract;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseMVPActivity<ChangePasswordContract.Presenter> implements ChangePasswordContract.View {

    @BindView(R.id.iv_changeback)
    ImageView ivChangeback;
    @BindView(R.id.et_oldpassword)
    ClearableEditText etOldpassword;
    @BindView(R.id.et_newpassword)
    ClearableEditText etNewpassword;
    @BindView(R.id.et_confirm)
    ClearableEditText etConfirm;
    @BindView(R.id.bt_change)
    Button btChange;
    @BindView(R.id.pb_change)
    ProgressBar pbChange;
    @BindView(R.id.iv_oldhide)
    ImageView ivOldhide;
    @BindView(R.id.iv_newhide)
    ImageView ivNewhide;
    @BindView(R.id.iv_confirmhide)
    ImageView ivConfirmhide;
    private boolean isConfrimHide = true;
    private boolean  isOldHide=true;
    private boolean isNewHide=true;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initViewsAndEvents() {
        setHide(true, etOldpassword, ivOldhide);
        setHide(true, etNewpassword, ivNewhide);
        setHide(true, etConfirm, ivConfirmhide);
        etConfirm.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        etNewpassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        etOldpassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});




    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }
    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected ChangePasswordContract.Presenter createPresenter() {
        return new ChangerPasswordPersenter();
    }

    @Override
    public void changeSuccess(String password) {
        ToastUtils.showToast(this,R.string.change_success);
        btChange.setVisibility(View.VISIBLE);
        pbChange.setVisibility(View.GONE);
        preferenceUtil.setPassword(password);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        },500);
    }
    @Override
    public void changeFaild(String error) {
        ToastUtils.showToast(this,error);
        btChange.setVisibility(View.VISIBLE);
        pbChange.setVisibility(View.GONE);

    }

    @OnClick({R.id.iv_changeback, R.id.bt_change,R.id.iv_oldhide,R.id.iv_newhide,R.id.iv_confirmhide})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_changeback:
                finish();
                break;
            case R.id.bt_change:
                String newPassword = etNewpassword.getText().toString().trim();
                String oldPassword=etOldpassword.getText().toString().trim();
                String confimPassword=etConfirm.getText().toString().trim();
                if(TextUtils.isEmpty(newPassword)
                        ||TextUtils.isEmpty(oldPassword)
                        ||TextUtils.isEmpty(confimPassword)){
                    ToastUtils.showToast(this,R.string.cannot_beempty);
                    return;
                }
                if(!newPassword.equals(confimPassword)){
                    ToastUtils.showLongToast(this, R.string.wrong_password);
                    return;
                }
                btChange.setVisibility(View.GONE);
                pbChange.setVisibility(View.VISIBLE);
                mPersenter.changePassword(oldPassword,newPassword);
                break;
            case R.id.iv_oldhide:
                isOldHide=!isOldHide;
                setHide(isOldHide, etOldpassword, ivOldhide);
                break;
            case R.id.iv_newhide:
                isNewHide=!isNewHide;
                setHide(isNewHide,etNewpassword,ivNewhide);
                break;
            case R.id.iv_confirmhide:
                isConfrimHide=!isConfrimHide;
                setHide(isConfrimHide,etConfirm,ivConfirmhide);
                break;
        }
    }

    public void setHide(boolean hide, ClearableEditText editText, ImageView imageView) {
        if (hide) {
            imageView.setSelected(false);
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);//设置密码不可见
        } else {
            imageView.setSelected(true);
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//设置密码可见
        }
    }


}
